package com.rsjgroup.water.service.impl;

import com.rsjgroup.water.mapper.UserMapper;
import com.rsjgroup.water.pojo.User;
import com.rsjgroup.water.service.IUserService;
import com.rsjgroup.water.service.exception.UserNotFoundException;
import com.rsjgroup.water.service.exception.updateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void updateAvatar(User user) {
        Integer uid = user.getUid();
        User userByUid = userMapper.findUserByUid(uid);
        if (userByUid == null) {
            throw new UserNotFoundException("用户信息未找到");
        }
        if (userByUid.getIsDelete() != 0) {
            throw new UserNotFoundException("用户已注销");
        }
        Integer rows = userMapper.updateAvatar(user);
        if (rows != 1) {
            throw new updateException("用户更新头像产生了未知异常");
        }
    }

    @Override
    public void updateUsername(User user) {
        Integer uid = user.getUid();
        User userByUid = userMapper.findUserByUid(uid);
        if (userByUid == null) {
            throw new UserNotFoundException("用户信息未找到");
        } else if (userByUid.getIsDelete() == 1) {
            throw new UserNotFoundException("用户已注销");
        }
        Integer rows = userMapper.updateUsername(user);
        if (rows != 1) {
            throw new updateException("用户更新用户名产生了未知异常");
        }
    }
}
